//
//  LKSinggleSelected.m
//  OOP
//
//  Created by rimi on 15/10/9.
//  Copyright (c) 2015年 Liukai. All rights reserved.
//

#import "LKSinggleSelectionControl.h"
enum {
    ButtonTag = 100
};
@interface LKSinggleSelectionControl ()
@property (nonatomic, strong) NSArray *items;
- (void)initializeAppearence;
@end
@implementation LKSinggleSelectionControl


- (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items {
    self = [super initWithFrame:frame];
    if (self) {
        self.items = items;
        [self initializeAppearence];
    }
    return self;
}

- (void)initializeAppearence {
    
    [self.items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
   
        CGFloat buttonWidth = 50;
        CGFloat interval = - 5;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:obj forState:UIControlStateNormal];
        if (idx == 0) {
            button.selected = YES;
        }
        button.frame = CGRectMake(idx * (buttonWidth + interval), 0, 30, 20);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(handleEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = ButtonTag + idx;
        [self addSubview:button];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"mon"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"tues"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"wednes"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"thurs"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"fri"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"satur"];
         [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"sun"];

        
    }];
}

- (void)handleEvent:(UIButton *)sender {
    sender.selected = !sender.selected;
//    UIButton *selectedButton = (UIButton *)[self viewWithTag:_selectedIndex + ButtonTag];
//    selectedButton.selected = NO;
//    sender.selected = YES;
//    _selectedIndex = sender.tag - ButtonTag;
    if (sender.selected) {
        if (sender.tag == 100) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"mon"];
        }
        else if(sender.tag == 101) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"tues"];
        }
        else if(sender.tag == 102) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"wednes"];
        }
        else if(sender.tag == 103) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"thurs"];
        }
        else if(sender.tag == 104) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"fri"];
        }
        else if(sender.tag == 105) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"satur"];
        }
        else if(sender.tag == 106) {
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"sun"];
        }
    }
    if (!sender.selected) {
        if (sender.tag == 100) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"mon"];
        }
        else if(sender.tag == 101) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"tues"];
        }
        else if(sender.tag == 102) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"wednes"];
        }
        else if(sender.tag == 103) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"thurs"];
        }
        else if(sender.tag == 104) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"fri"];
        }
        else if(sender.tag == 105) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"satur"];
        }
        else if(sender.tag == 106) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"sun"];
        }
    }
    
    
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"mon"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:100];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"tues"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:101];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"wednes"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:102];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"thurs"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:103];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"fri"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:104];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"satur"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:105];
//        button.selected = NO;
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"sun"] isEqual:@"YES"]) {
//        UIButton *button = (UIButton *)[self viewWithTag:106];
//        button.selected = NO;
//    }

}
@end
